package mipushdemo.xiaomi.com.diyitonghang.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.regex.Pattern;

import butterknife.BindView;
import mipushdemo.xiaomi.com.diyitonghang.R;
import mipushdemo.xiaomi.com.diyitonghang.base.BaseActivity;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.LoginBean;
import mipushdemo.xiaomi.com.diyitonghang.presenter.contract.homecontract.IMaContract;
import mipushdemo.xiaomi.com.diyitonghang.presenter.impl.loginimpl.MaPresenterImpl;

public class LoginActivity extends BaseActivity implements View.OnClickListener,IMaContract.IMaView<IMaContract.IMaPresenter> {


    @BindView(R.id.et_userPhone)
    EditText etUserPhone;
    @BindView(R.id.et_yanZhengMa)
    EditText etYanZhengMa;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.loginToolbar)
    Toolbar loginToolbar;
    @BindView(R.id.tv_getYanZhengMa)
    TextView tvGetYanZhengMa;
    boolean bo = false;
    int time = 60;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    int i = time--;
                    if(i == 0){
                        tvGetYanZhengMa.setText("获取验证码");
                        tvGetYanZhengMa.setOnClickListener(LoginActivity.this);
                        time = 60;
                    }else{
                        tvGetYanZhengMa.setText(i+"");
                        handler.sendEmptyMessageDelayed(0,1000);
                        tvGetYanZhengMa.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                return;
                            }
                        });
                    }
                    break;
            }
        }
    };
    private IMaContract.IMaPresenter maPresenter = new MaPresenterImpl(this);

    String header = "application/json";

    @Override
    protected int windowColor() {
        return R.color.colorWhite;
    }

    @Override
    protected void init() {
        loginToolbar.setTitle("");
        setSupportActionBar(loginToolbar);
        loginToolbar.setNavigationIcon(R.drawable.close);
        loginToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        changeStatusBarTextColor(true);
        tvGetYanZhengMa.setOnClickListener(this);
        btnLogin.setOnClickListener(this);
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
    private boolean matchPhone(String text) {
        if (Pattern.compile("^((1[358][0-9])|(14[57])|(17[0678]))\\d{8}$").matcher(text).matches()) {
            return true;
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                String phoneNum = etUserPhone.getText().toString();
                String yanZhengMa = etYanZhengMa.getText().toString();
                if(phoneNum.isEmpty()){
                    Toast.makeText(this, "手机号不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(yanZhengMa.isEmpty()){
                    Toast.makeText(this, "验证码不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!matchPhone(phoneNum)){
                    Toast.makeText(this, "手机号输入有误,请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }
                maPresenter.login(header,phoneNum,yanZhengMa);
                break;
            case R.id.tv_getYanZhengMa:
                String phone = etUserPhone.getText().toString();
                if(phone.isEmpty()){
                    Toast.makeText(this, "手机号不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(!matchPhone(phone)){
                    Toast.makeText(this, "手机号输入有误,请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }
                maPresenter.loginMa(header,phone);
                break;
        }
    }


    @Override
    public void showData(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        if(message.equals("验证码已发送")){
            handler.sendEmptyMessage(0);
        }
    }

    @Override
    public void showLoginData(LoginBean message) {
        Toast.makeText(this, message.getMessage(), Toast.LENGTH_SHORT).show();
       if(message.getCode() == 0){
           SharedPreferences preferences = getSharedPreferences("id", MODE_PRIVATE);
           SharedPreferences.Editor edit = preferences.edit();
           edit.putString("userId",message.getData().getUserId());
           edit.commit();
           Intent intent = new Intent(this, MainActivity.class);
           startActivity(intent);
           finish();
       }
    }

    @Override
    public void setPresenter(IMaContract.IMaPresenter iMaPresenter) {
        maPresenter = iMaPresenter;
    }

}
